//
//  FileSocket.m
//  TcpTest_Client
//
//  Created by scarlettsun on 12-8-3.
//  Copyright (c) 2012年 tencent. All rights reserved.
//
#import "FileSocket.h"
#import "FileEntity.h"
#import "FileWindowController.h"
#import "FileListenSocket.h"
#import "MainWindowController.h"
#import "MacrosDefine.h"


@implementation FileSocket
@synthesize fileAsynSocket;
@synthesize delegate;


-(void) dealloc
{
    [self.fileAsynSocket disconnect];
    buffer = nil;
    self.fileAsynSocket = nil;
}

-(FileSocket *) initWithSock:(GCDAsyncSocket *)sock
{
    self.fileAsynSocket = sock ;
    self.fileAsynSocket.delegate = self;
    packetLength = LENGTH;
    buffer = nil;
    readMode = Readlenth;
    return self;
}

- (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket;
{}

//GCDAsyncSocket  not respond to this function 
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port;
{
     NSLog(@"file socket didConnectToHost: ip is %@,port is %u\r\n",host,port);
    [self.fileAsynSocket readDataToLength:LENGTH withTimeout:-1 tag:0];
}
- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag;
{
    NSLog(@"did write data ,tag is %lu\r\n",tag);
    [delegate sendFIleContent];
}
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag;
{
    switch (readMode) 
    {
        case Readlenth:
        {
            packetLength =*((NSUInteger *)([data bytes]));
            //NSLog(@"packet lenth is  %lu",packetLength);
            [sock readDataToLength:packetLength withTimeout:-1 tag:0];
            readMode = ReadContent;
            break; 
        }
        case ReadContent: 
        {  
            readMode = Readlenth;
            NSUInteger fun =*((NSUInteger *)([data bytes])); 
           if (fun == CMD_FUN_FILE) 
           {
               NSUInteger subfun =*((NSUInteger *)([data bytes]+8));
                switch (subfun) 
                {
                    case CMD_FUN_FILE_REQUEST:    //accept a new file head info 
                    {
                        [delegate receiveFileInfo:data];
                        break;
                    }
                    case CMD_FUN_FILE_CONTENT:  //accept file content 
                    {
                        [delegate receiveFileContent:data];
                        break;
                    }
                    case CMD_FUN_FILE_ACCEPT:
                    {
                        [delegate fileAccept];
                        break;
                    }
                    case CMD_FUN_FILE_REJECT:
                    {
                        [delegate fileReject];
                        break;
                    }
                    case CMD_FUN_FILE_CANCEL:
                    {
                        [delegate fileCancel];
                        break;
                    }
                    default:
                        break;
                }
           }
           else 
           {
               NSLog(@"wrong packet received,len is %lu",[data length]);
           }
            [sock readDataToLength:LENGTH withTimeout:-1 tag:0];
            packetLength = 0;
            break;
        }
        default:
        { 
            [sock readDataToLength:LENGTH withTimeout:-1 tag:0];
            break;
        }
    }
}

- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err;
{
    [self.delegate fileSocketDisConnect];
    self.delegate = nil;
    self.fileAsynSocket = nil;
    buffer = nil;
    @synchronized([FileListenSocket fileSocketsQueue])
    {
        [[FileListenSocket fileSocketsQueue] removeObject:self];
    }
}

@end
